package com.retailshop.shopuser.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.retailshop.shopuser.common.DateUtil;
import com.retailshop.shopuser.entity.*;
import com.retailshop.shopuser.mapper.OwnerConfigMapper;
import com.retailshop.shopuser.mapper.UserOwnerMapper;
import com.retailshop.shopuser.service.UserOwnerService;
import com.retailshop.shopuser.service.UserRedisService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("UserOwnerService")
public class UserOwnerServiceImpl implements UserOwnerService {

    @Autowired
    UserOwnerMapper userOwnerMapper;

    @Autowired
    OwnerConfigMapper ownerConfigMapper;

    @Autowired
    UserRedisService userRedisService;

    @Autowired
    RedisTemplate redisTemplate;

    OwnerConfig ownerConfig=null;

    public void SetMsg(String key,String value)
    {
        userRedisService.SetMsg(key,value);
    }

    public void SetState(String key,String value)
    {
        userRedisService.SetState(key,value);
    }

    public Boolean GetState(String key)
    {
        return userRedisService.GetState(key);
    }

    @Override
    public OwnerUser selectByPrimaryKey(Integer id)
    {
        return userOwnerMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<OwnerShareUser> getOwnerList(@Param("id")int id, @Param("rankId")int rankId, @Param("rankShopId")int rankShopId) {
        if(id>0)
        {
            if(rankId==0)
            {
                return userOwnerMapper.selectOwnerUserShareList(id);
            }
            else
            {
                if(rankShopId==0)
                {
                    return userOwnerMapper.selectOwnerUserShareRankList(id,rankId);
                }
                else {
                    return userOwnerMapper.selectOwnerUserShareRankShopList(id, rankId,rankShopId);
                }
            }
        }
        return null;
    }

    @Override
    public List<OwnerUser> getList(@Param("id")Integer id,@Param("no")String no,@Param("name")String name,@Param("phone")String phone,@Param("rankServiceId")Integer rankServiceId,@Param("rankShopId")Integer rankShopId) {
        return userOwnerMapper.selectList(id,no,name,phone,rankServiceId,rankShopId);
    }

    @Override
    public List<OwnerUser> getList1(@Param("id")Integer id,@Param("no")String no,@Param("name")String name,@Param("phone")String phone) {
        return userOwnerMapper.selectList1(id,no,name,phone);
    }


    @Override
    public boolean settlementRealpay() {
        SetMsg("realpayMsg", "开始核算销售");
        SetState("realpayState", "1");
        if (!GetState("realpayControl")) {

            SetMsg("realpayMsg", "开始实习店主核算销售");
            //实习店主
            List<OwnerUser> ownerUserList = userOwnerMapper.selectListI();
            if (ownerUserList != null) {
                for (int i = 0; i < ownerUserList.size(); i++) {
                    SetMsg("realpayMsg", "开始核算实习店主销售:" + ownerUserList.size() + "-" + (i + 1) + "-" + ownerUserList.get(i).getId());
                    Internship(ownerUserList.get(i));
                }
            }

            SetMsg("realpayMsg", "完成实习店主核算销售");

            SetMsg("realpayMsg", "开始微店店主核算销售");
            //微店店主
            ownerUserList.clear();
            ownerUserList = userOwnerMapper.selectListOwner(3);
            if (ownerUserList != null) {
                for (int i = 0; i < ownerUserList.size(); i++) {
                    SetMsg("realpayMsg", "开始核算微店店主销售:" + ownerUserList.size() + "-" + (i + 1) + "-" + "-" + ownerUserList.get(i).getId());
                    Owner(ownerUserList.get(i));
                    break;
                }
            }

            SetMsg("realpayMsg", "完成微店店主核算销售");

        }
        SetMsg("realpayMsg", "完成核算销售");
        SetState("realpayState", "0");
        return true;
    }

    public void Internship(OwnerUser ownerUser)
    {
        userOwnerMapper.setOwnerAll(ownerUser.getId(),ownerUser.getInternshipTime(), DateUtil.DateAddDay(ownerUser.getInternshipTime(),30));
    }

    public void Owner(OwnerUser ownerUser)
    {
        userOwnerMapper.setOwnerAll(ownerUser.getId(),DateUtil.getFirstDay(),DateUtil.getNextDay());
    }

    @Override
    public boolean clearUserShopShare() {
        userOwnerMapper.clearUserShopShare();
        return true;
    }


    @Override
    public boolean lowerOwner() {
        SetMsg("shareMsg", "开始整理分享数据");
        SetState("shareState", "1");
        if (!GetState("shareControl")) {
            //清空UserShopShare
            SetMsg("shareMsg", "清空UserShopShare");
            userOwnerMapper.clearUserShopShare();

            SetMsg("shareMsg", "开始整理实习店主分享数据:");

            //实习店主
            List<OwnerUser> ownerUserList = userOwnerMapper.selectListI();
            if (ownerUserList != null) {
                for (int i = 0; i < ownerUserList.size(); i++) {
                    SetMsg("shareMsg", "开始整理分享数据:" + ownerUserList.size() + "-" + (i + 1) + "-" + ownerUserList.get(i).getId());
                    InternshipLowerOwner(ownerUserList.get(i));
                    break;
                }
            }

            SetMsg("shareMsg", "开始整理店主分享数据:");
            //店主
            ownerUserList.clear();
            ownerUserList = userOwnerMapper.selectListOwner(3);
            if (ownerUserList != null) {
                for (int i = 0; i < ownerUserList.size(); i++) {
                    SetMsg("shareMsg", "开始整理分享数据:" + ownerUserList.size() + "-" + (i + 1) + "-" + ownerUserList.get(i).getId());
                    OwnerLowerOwner(ownerUserList.get(i),2);
                    break;
                }
            }
        }
        SetMsg("shareMsg", "完成整理分享数据");
        SetState("shareState", "0");
        return true;
    }

    @Override
    public boolean InternshipLowerOwner(OwnerUser ownerUser)
    {
        getConfigOwner();

        userOwnerMapper.insertShareI(ownerUser.getId());
        System.out.println("getOwnerday1:"+ownerConfig.getOwnerday());
        System.out.println("getId:"+ownerUser.getId());
        System.out.println("getInternshipTime:"+ownerUser.getInternshipTime());
        System.out.println("getInternshipTime:"+DateUtil.DateAddDay(ownerUser.getInternshipTime(),ownerConfig.getOwnerday()));

        userOwnerMapper.setOwnerAll(ownerUser.getId(),ownerUser.getInternshipTime(), DateUtil.DateAddDay(ownerUser.getInternshipTime(),ownerConfig.getOwnerday()));
        System.out.println("完成");
        return true;
    }

    @Override
    public boolean OwnerLowerOwner(OwnerUser ownerUser,int rankId)
    {
        userOwnerMapper.insertShare(ownerUser.getId(),rankId);
        userOwnerMapper.setOwnerAll(ownerUser.getId(),DateUtil.getFirstDay(),DateUtil.getNextDay());
        return true;
    }

    @Override
    public boolean checkOwner()
    {
        getConfigOwner();

        SetMsg("checkOwnerMsg", "开始检查店主");
        SetState("checkOwnerState", "1");
        //检查实习店主
        if (!GetState("checkOwnerControl")) {

            //实习店主
            SetMsg("checkOwnerMsg", "开始检查实习店主");
            List<OwnerUser> ownerUserList = userOwnerMapper.selectListCI();
            if (ownerUserList != null) {
                for (int i = 0; i < ownerUserList.size(); i++) {
                    SetMsg("checkOwnerMsg", "开始检查实习店主:" + ownerUserList.size() + "-" + (i + 1) + "-" + ownerUserList.get(i).getId());
                    checkOwnerInternship1(ownerUserList.get(i));
                }
            }

            //检查店主

        }
        return true;
    }

    @Override
    public boolean checkOwnerInternship(Integer id)
    {
        OwnerUser ownerUser = userOwnerMapper.selectIId(id);
        if(ownerUser!=null) {

            //重建推荐
            InternshipLowerOwner(ownerUser);

            ownerUser = userOwnerMapper.selectIId(id);

            //判断是否合格
            checkOwnerInternship1(ownerUser);
        }
        return true;
    }

    @Override
    public boolean checkOwnerInternshipAll()
    {
        System.out.println("开始检查");
        List<OwnerUser> ownerUserList = userOwnerMapper.selectInOrderList();
        for (int i=0;i<ownerUserList.size();i++) {
            OwnerUser ownerUser = ownerUserList.get(i);
            System.out.println("开始:"+ownerUser.getId());
            if (ownerUser != null) {
                //重建推荐
                InternshipLowerOwner(ownerUser);

                ownerUser = userOwnerMapper.selectIId(ownerUser.getId());

                //判断是否合格
                checkOwnerInternship1(ownerUser);
            }
        }
        return true;
    }

    public void getConfigOwner()
    {
        if(ownerConfig==null) {
            System.out.println("读取配置文件");
            //读取配置
            Object redisOwnerConfig = redisTemplate.opsForValue().get("OwnerConfig");
            if (redisOwnerConfig != null) {
                //System.out.println("redisOwnerConfig:"+redisOwnerConfig.toString());
                ownerConfig = JSON.parseObject(redisOwnerConfig.toString(), new TypeReference<OwnerConfig>() {
                });
            }
        }
        if(ownerConfig==null)
        {
            ownerConfig = ownerConfigMapper.selectByPrimaryKey(1);
            redisTemplate.opsForValue().set("OwnerConfig",JSON.toJSONString(ownerConfig));
        }
    }

    public boolean checkOwnerInternship1(OwnerUser ownerUser)
    {
        boolean bl =false;
        System.out.println("检查第一条件");
        //检查第一条件
        if(ownerUser.getOrderAmount()>=ownerConfig.getOwneramount1())
        {
            bl=true;
        }
        System.out.println("检查第二条件");
        //检查第二条件
        if(ownerUser.getOrderAmount()>=ownerConfig.getOwneramount2())
        {
            bl=true;
        }
        System.out.println("检查第三条件");
        //检查第三条件


        if(ownerUser.getOrderUserCount()>=ownerConfig.getOwnerusernum() && ownerUser.getSharerCount()>=ownerConfig.getOwnersharenum() && ownerUser.getUserCount()>=ownerConfig.getOwnernum())
        {
            bl=true;
        }

        if(bl)
        {
            //更新
            userOwnerMapper.setShopOwnerQualified(ownerUser.getId());
        }

        return true;
    }

    @Override
    public boolean checkStar()
    {
        getConfigOwner();
        userOwnerMapper.setUserStar(ownerConfig.getStaru1(),ownerConfig.getStars1(),ownerConfig.getStaru2(),ownerConfig.getStars2());
        return true;
    }

    @Override
    public boolean checkActivation()
    {
        getConfigOwner();
        userOwnerMapper.setActivation(ownerConfig.getOldnum());
        return true;
    }
}